package com.atguigu.gmall.order.listener;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.common.constant.MqConst;
import com.atguigu.gmall.model.enums.OrderStatus;
import com.atguigu.gmall.model.enums.ProcessStatus;
import com.atguigu.gmall.order.service.OrderInfoService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * @author: 李旭
 * @create: 2021-09-14 09:09
 * <p>
 * 白盒 测试工程师
 * 黑盒 测试工程师 结果
 **/
@Component
public class OrderInfoListener {

    @Autowired
    private OrderInfoService orderInfoService;

    //接收取消订单的MQ
    @RabbitListener(queues = MqConst.QUEUE_ORDER_CANCEL)
    public void receiverMessage(Long orderId, Channel channel, Message message) {
        //channel.basicQos(3);//局部设置 预取值   以局部为准
        try {
            System.out.println("订单ID：" + orderId);//
            orderInfoService.closedOrder(orderId);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //接收更新订单
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(value = MqConst.QUEUE_PAYMENT_PAY, durable = "true", autoDelete = "false"),
                    exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_PAYMENT_PAY),
                    key = {MqConst.ROUTING_PAYMENT_PAY}
            )
    })
    public void receiverUpdateOrderMessage(Long orderId, Channel channel, Message message) {
        try {
            System.out.println("订单ID：" + orderId);//
            //由未支付改为已支付
            orderInfoService.updateOrderStatusPaid(orderId, ProcessStatus.PAID);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //接收  商品减库结果消息
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(value = MqConst.QUEUE_WARE_ORDER, durable = "true", autoDelete = "false"),
                    exchange = @Exchange(value = MqConst.EXCHANGE_DIRECT_WARE_ORDER),
                    key = {MqConst.ROUTING_WARE_ORDER}
            )
    })
    public void receiverDescStockMessage(String json, Channel channel, Message message) {
        try {
            System.out.println("json：" + json);//
            Map map = JSONObject.parseObject(json, Map.class);
            Object status = map.get("status");
            Object orderId = map.get("orderId");
            if ("DEDUCTED".equals(status)) {
                System.out.println("扣减成功");
                //更新订单状态为待发货
                //Object  - Integer String    -> Long   类型转换 ：  类型转换异常
                //Object   Integer String Boolean
                orderInfoService.updateOrderStatus(Long.parseLong(String.valueOf(orderId)),
                        ProcessStatus.WAITING_DELEVER);

            } else {
                System.out.println("已付款，库存超卖");
                //写日志
                orderInfoService.updateOrderStatus(Long.parseLong(String.valueOf(orderId)),
                        ProcessStatus.STOCK_EXCEPTION);
            }
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
